package com.et114.modules.system.dictionary;

import java.io.IOException;
import java.util.ArrayList;
//import java.util.Hashtable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
//import java.util.Map;
//import java.util.Set;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


import com.et114.components.ehcache.EhCacheUtils;
import com.et114.core.common.AppContext;
import com.et114.modules.system.dictionary.vo.BizOption;
import com.et114.modules.system.dictionary.vo.BizType;


/**
 * @author  guanhw
 *  */
public class DictionaryCache {
	protected Log logger = LogFactory.getLog( this.getClass( ) );
	public final static String		BIZOPTION_STATUS_CLOSE	= "0";
	public final static String		BIZOPTION_STATUS_OPEN	= "1";
	public final static String		BIZOPTION_STATUS_ALL	= "2";
	private static DictionaryCache	instance;
	private static DictionaryEhCacheManager cacheManager ; 
	
	private DictionaryCache ( ) {
		try {
			cacheManager = ( DictionaryEhCacheManager ) AppContext .getBean (  "dictionaryEhCacheManager" );
		}
		catch ( Exception e ) {
			logger.error (  e.getMessage ( ) , e ) ; 
		}
	}
	
	public void after( ) throws Exception  { // TODO
	}
	
	public static  DictionaryCache getInstance ( ) {
		if ( instance == null ){
			instance = new DictionaryCache ( );
		}
		return instance;
	}
	
	/**
	 * ˢ��ҵ������ֵ仺��
	 * @throws IOException 
	 * @throws CacheException 
	 */
	public synchronized void refresh ( ) throws Exception {
			cacheManager.initDicBizCache ( ) ; 
	}
	
	/**
	 * ���ҵ�����ѡ��ID����ҵ�����ѡ�����
	 * 
	 * @param bizOptionId
	 * @return
	 */
	/*
	public BizOption getBizOption ( String bizOptionId ) throws Exception  {
		return ( BizOption ) EhCacheUtils.get ( cacheManager.getCacheBizOption ( ) , bizOptionId ) ;
		
	}
	 */
	
	/**
	 * ���ҵ�����ѡ��ID����ҵ�����ѡ������
	 * 
	 * @param bizOptionId
	 * @return
	 */

	
	public String getBizOptionName ( String biztypekey , String bizoptionvalue ) throws Exception  {
		BizType bizType = getBizType ( biztypekey ) ; 
		for ( Iterator iter = bizType.getOptions ( ).iterator ( ) ; iter
				.hasNext ( ) ; ) {
			BizOption option = ( BizOption ) iter.next ( );
			if ( /*option.getLanguage ( ).equals (
					PrincipalThreadLocal.getName ( ) ) 
					&& */ option.getOptionValue ( ).equals ( bizoptionvalue ) ) {
				return option.getOptionName ( );
			}
		}
		return "";
	}
	
	/**
	 * ���ҵ�����ѡ��ID����ҵ�����ѡ���Ȩ��
	 * 
	 * @param bizOptionId
	 * @return
	 */

	/**
	 * ���ҵ������key����ҵ�����Ͷ���
	 * 
	 * @param bizTypeId
	 * @return
	 */
	public BizType getBizType ( String bizTypeKey ) throws Exception {
		return ( BizType )EhCacheUtils.get ( cacheManager.getCacheBizType ( ) , bizTypeKey )  ; 
	}
	
	/**
	 * ���ҵ������ID���ش�ҵ��������ҵ�����ѡ���б�
	 * 
	 * @param bizTypeId
	 * @param incInvalid
	 *            �Ƿ��(status��־Ϊ��Ч��ҵ�����ѡ��
	 * @return
	 */
	public List listBizOptions ( String biztypekey , boolean incInvalid ) throws Exception  {
		BizType bizType = ( BizType ) getBizType( biztypekey ) ;
		List options = new ArrayList ( );
		if ( bizType == null )
			return new ArrayList ( );
		for ( Iterator iter = bizType.getOptions ( ).iterator ( ) ; iter
				.hasNext ( ) ; ) {
			BizOption option = ( BizOption ) iter.next ( );
			/*
			if ( option.getLanguage ( ).equals (
					PrincipalThreadLocal.getName ( ) ) ) {
					*/
				if ( incInvalid ) {
					options.add ( option );
				} else if ( option.getStatus ( )
						.equals ( BIZOPTION_STATUS_OPEN ) ) {
					options.add ( option );
				} else {
				}
			//}
		}
		return options;
	}
	
	public List listBizOptions ( String biztypekey , boolean incInvalid , String locate_country ) throws Exception  {
		BizType bizType = ( BizType ) getBizType( biztypekey ) ;
		List options = new ArrayList ( );
		if ( bizType == null )
			return new ArrayList ( );
		for ( Iterator iter = bizType.getOptions ( ).iterator ( ) ; iter
				.hasNext ( ) ; ) {
			BizOption option = ( BizOption ) iter.next ( );
			if ( option.getLanguage ( ).equals ( locate_country ) ) {
				if ( incInvalid ) {
					options.add ( option );
				} else if ( option.getStatus ( )
						.equals ( BIZOPTION_STATUS_OPEN ) ) {
					options.add ( option );
				} else {
				}
			}
		}
		return options;
	}	
	
	
	/**
	 * ���ҵ������Key���ش�ҵ��������ҵ�����ѡ���б?ֻ��status��־Ϊ��Ч��
	 * 
	 * @param bizTypekey
	 * @return
	 */
	public List listBizOptions ( String bizTypekey ) throws Exception   {
		return listBizOptions ( bizTypekey , false );
	}
	
	public List listBizOptions( String bizTypekey , String locate_country ) throws Exception  {
		return listBizOptions ( bizTypekey , false , locate_country );
	}
	
	public List listBizTypes ( ) {
		return cacheManager.getCacheBizType ( ).getKeys ( );
	}
	
	public  DictionaryEhCacheManager getCacheManager ( ) {
		return cacheManager;
	}

	public  void setCacheManager ( DictionaryEhCacheManager cacheManager ) {
		DictionaryCache.cacheManager = cacheManager;
	}
	
	
	public Map  convertListToMap ( List < BizOption > bos )
			throws Exception {
		Map  ms = new HashMap  ( bos
				.size ( ) );
		for ( BizOption bo : bos ) {
			ms.put ( bo.getOptionName ( ) , bo.getOptionValue ( ) );
		}
		return ms;
	}
	
	public void putBizTypeInCache ( BizType bizType ) {
		Cache cache = cacheManager.getCacheBizType ( );
		EhCacheUtils.put ( cache, bizType ) ;
	}
	
	public void removeBizTypeFromCache ( BizType bizType ) {
		Cache cache = cacheManager.getCacheBizType ( );
		EhCacheUtils.remove ( cache, bizType.getKey() ) ;
	}
	
	public void modifyBizTypeInCache ( BizType bizType ) {
		//removeBizTypeFromCache ( bizType );
		putBizTypeInCache ( bizType );
	}
	
	
}
